package fun.coding.design.pattern;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * This is a simple example of a decorator pattern, Java uses this pattern a lot in java.io package. 
 * One of the obvious reasons is this can significantly reduce the number of classes due to different combinations,
 * e.g., I want to use BufferInputStream with LineNumberInputStream, I could just keep decorating those
 * BufferInputStream(new LineNumberInputStream(new InputStream)) instead of defining 4 different sub-classes, 
 * Like BufferInputWithLineNumberStream. 
 * 
 * One cons of using decorator pattern is it will create many small classes, which could be scary at first for new engineers. 
 */
public class LowerCaseInputStream extends FilterInputStream{

    protected LowerCaseInputStream(InputStream in) {
        super(in);
    }
    
    @Override
    public int read() throws IOException {
        int c = super.read();
        // System.out.println("Value byte (0-255) is " + c);
        
        // read() method will return -1 if it reaches the end of the stream
        return (c == -1 ? c : Character.toLowerCase(c));
    }
}
